package cn.biplam.back.operator.tree;

import java.util.ArrayList;

import cn.biplam.back.sql.dml.Delete;
import cn.biplam.common.client.DataFilter;
import cn.biplam.common.client.DbResource;
import cn.biplam.common.client.Operator;
import cn.biplam.common.opcode.SysManageCode.TreeIndexCode;
import cn.biplam.common.tools.TOOLS;
import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.common.wrapper.sqlresult.SqlResult;
import cn.biplam.common.wrapper.tree.TreeIndexFields;

/**
 *  索引树数据操作—删除节点
 * @author XiaoRuxing Nov 9, 20094:47:15 PM
 * @version 1.0
 */
public class OptIndexTreeDelete extends IndexTree{

	
	protected OptIndexTreeDelete(String userId, String tableName,
			TreeIndexFields fields, DbResource dbResource) {
		super(userId, tableName, fields, dbResource);
	}

	/**
	 *  删除虚根节点,传入节点ID
	 */
	protected void deleteVirtualNode(ArrayList<String> sql,DataFilter filter) throws ErrorMessage{
		//设置删除条件为节点ID为传入节点ID值
		String sNodeID = filter.getValue(this.fields.getNodeId());
		// 创建删除SQL
		deleteSql(sql, sNodeID);
	}
	
	/**
	 *  根据索引ID删除节点
	 */
	protected void deleteNodeByIndex(ArrayList<String> sql,DataFilter filter) throws ErrorMessage{
		DataFilter sFilter = new DataFilter();
		sFilter.putAll(filter);
		/** 获取当前节点信息 */
		SqlResult sr=Operator.get(TreeIndexCode.Select_Node, userId, sFilter).getSelectResult();
		_deleteNode(sql, sFilter, sr);
	}

	/**
	 * 根据真实Id删除节点，当节点有子节点时不允许删除
	 * @param filter 删除节点过滤条件
	 *           	 过滤条件为节点的对应的真实ID
	 */
	protected void deleteNode(ArrayList<String> sql,DataFilter filter) throws ErrorMessage{
		DataFilter sFilter = new DataFilter();
		sFilter.putAll(filter);
		/** 获取当前节点信息 */
		SqlResult sr=Operator.get(TreeIndexCode.Select_By_Rel_ID, userId, sFilter).getSelectResult();
		_deleteNode(sql, sFilter, sr);
	}
	
	/**
	 *  根据索引节点ID删除根节点
	 */
	protected void deleteRootByIndexId(ArrayList<String> sql,DataFilter filter) throws ErrorMessage{
		DataFilter sFilter = new DataFilter();
		sFilter.putAll(filter);
		/** 获取当前节点信息 */
		SqlResult sr=Operator.get(TreeIndexCode.Select_Node, userId, sFilter).getSelectResult();
		deleteRoot(sql, sFilter, sr);
	}

	/**
	 * 根据节点对应真实ID删除根节点
	 */
	protected void deleteRoot(ArrayList<String> sql,DataFilter filter) throws ErrorMessage{
		DataFilter sFilter = new DataFilter();
		sFilter.putAll(filter);
		/** 获取当前节点信息 */
		SqlResult sr=Operator.get(TreeIndexCode.Select_By_Rel_ID, userId, sFilter).getSelectResult();
		deleteRoot(sql, sFilter, sr);
	}
	
	/**
	 *  删除节点
	 * @throws ErrorMessage
	 */
	private void _deleteNode(ArrayList<String> sql,DataFilter filter,SqlResult sr) throws ErrorMessage{
		String sNodeID=sr.get(0, this.fields.getNodeId());
		String sParentID=sr.get(0, this.fields.getNodeParent());
		String sequence=sr.get(0, this.fields.getNodeSequence());
		String operatorType = sr.get(0, this.fields.getOperatorType());
		//所有弟弟向前移动一位
		forwardSiblingSequence(sql, sParentID, sequence, operatorType);
		// 创建删除SQL
		deleteSql(sql, sNodeID);
	}

	/**
	 *  删除根节点
	 */
	private void deleteRoot(ArrayList<String> sql,DataFilter filter,SqlResult sr) throws ErrorMessage{
		int sequence=TOOLS.stringToInt(sr.get(0, this.fields.getNodeSequence()));
		String sNodeID=sr.get(0, this.fields.getNodeId());
		String nodeParent = sr.get(0, this.fields.getNodeParent());
		String nodeType = sr.get(0, this.fields.getNodeType());
		String operatorType = sr.get(0, this.fields.getOperatorType());
		//所有弟弟向前移动一位
		this.moveLBrother(sql, nodeParent, sequence, operatorType, nodeType, false);
		// 创建删除SQL
		deleteSql(sql, sNodeID);
	}
	
	//删除节点SQL
	private void deleteSql(ArrayList<String> sql,
			String nodeId) throws ErrorMessage{
		Delete delete=new Delete(this.tableName);
		StringBuilder sp = new StringBuilder();
		sp.append(this.fields.getNodeId());
		sp.append("='");
		sp.append(nodeId);
		sp.append("'");
		delete.setWhere(sp.toString());
		sql.add(delete.getSimpleSql());
	}
}
